package com.boc.image.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import com.boc.bus.control.BusOutputResult;
import com.boc.ibatis.config.IbatisSqlClient;
import com.boc.news.beans.EncryptMD5;

public class UserRegister {

	/**
	 * 判断用户名是否存在!
	 * 
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult checkUser(HttpServletRequest request,
			HttpServletResponse response, Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			Object count = IbatisSqlClient.sqlMapClient.queryForObject(
					"image_t_img_register.checkUser", m.get("cLoginname"));
			result.addValue("count", count);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 判断email是否使用
	 * 
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult checkUserEmail(HttpServletRequest request,
			HttpServletResponse response, Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			Object count = IbatisSqlClient.sqlMapClient.queryForObject(
					"image_t_img_register.checkUserEmail", m.get("cEmail"));
			result.addValue("count", count);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 用户注册
	 * 
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult userRegister(HttpServletRequest request,
			HttpServletResponse response, Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			// 判断验证码是否正确
			Object checkCode = request.getSession().getAttribute("checkCode");
			if (checkCode == null) {
				result.addValue("PN_RET", "3");
				result.addValue("PC_MSG", "验证码已失效!");
				return result;
			}
			if (!checkCode.toString().toLowerCase().equals(m.get("cYzm").toString().toLowerCase())) {
				result.addValue("PN_RET", "3");
				result.addValue("PC_MSG", "验证码输入不正确!");
				return result;
			}
			// 保存注册信息
			String sessionId = request.getSession().getId();
			String md5 = EncryptMD5.MD5(m.get("cLogin").toString() + m.get("cLoginpass").toString());
			m.put("cIp", request.getRemoteHost());
			m.put("cSessionid", sessionId);
			m.put("cLoginpass", md5);
			IbatisSqlClient.sqlMapClient.insert("image_procedure.userRegister", m);
			result.addValue("PN_RET", m.get("PN_RET"));
			result.addValue("PC_MSG", m.get("PC_MSG"));
			if (m.get("PN_RET").toString().equals("0")) {
				// 发送邮件到用户
				String webUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ request.getContextPath() +"/";
				sendEmail(m.get("cLogin").toString(),m.get("cEmail").toString(),md5,sessionId,webUrl);
			}
			request.getSession().setAttribute("register", "1");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	private static void sendEmail(String userName,String emailAddr,String md5,String sessionId,String webURL) {
		// SimpleEmail email = new SimpleEmail();
		HtmlEmail email = new HtmlEmail();
		email.setTLS(true);
		email.setHostName("smtp.qq.com");
		email.setAuthentication("87080234@qq.com", "1qaz2wsx3edc");

		try {
			email.addTo(emailAddr);
			email.setFrom("87080234@qq.com","好多图"); // 我方

			email.setSubject("《好多图》免费图片外链空间账户激活通知!"); // 标题

			email.setCharset("GB2312"); // 设置Charset

			// email.setMsg("<h7>这是一封Java程序发出的测试邮件。</h7>"); // 内容
			String url = webURL+"page/activation.jsp?UserName="+userName+"&code="+md5+"&key="+sessionId;;
			email.setHtmlMsg("<a href='"+url+"'>"+url+"</a>");

			email.send();

		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 用户注册信激活
	 * @param m
	 * @return
	 */
	public Map UserActivation(Map m){
		Map result = new HashMap();
		try {
			IbatisSqlClient.sqlMapClient.insert("image_procedure.userActivation", m);
			result.put("PN_RET", m.get("PN_RET"));
			result.put("PC_MSG", m.get("PC_MSG"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static void main(String[] args) {
		HtmlEmail email = new HtmlEmail();
		email.setTLS(true);
		email.setHostName("smtp.qq.com");
		email.setAuthentication("87080234@qq.com", "1qaz2wsx3edc");

		try {
			email.addTo("87014570@qq.com");
			email.setFrom("87080234@qq.com"); // 我方

			email.setSubject("好多图账户激活通知!"); // 标题

			email.setCharset("GB2312"); // 设置Charset

			// email.setMsg("<h7>这是一封Java程序发出的测试邮件。</h7>"); // 内容
			
			email.setHtmlMsg("fdsafdsads");

			email.send();

		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
}
